<?php

/**
 * commande actions.
 *
 * @package    TC5
 * @subpackage commande
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class commandeActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $personne = $this->getUser();
  	
    if ($personne->isSuperAdmin())
  		$this->commande_list = CommandePeer::doSelect(new Criteria());
    else {
    	$crit = new Criteria();
    	$crit->add(CommandePeer::PERSONNE_ID, $personne->getSfGuardUser()->getId(), Criteria::EQUAL);
    	$this->commande_list = CommandePeer::doSelect($crit);
    }
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->commande = CommandePeer::retrieveByPk($request->getParameter('id'));
    $this->forward404Unless($this->commande);
  	
    $ligne_commande = new LigneCommandePeer();
    $this->lignecommande_list =  $ligne_commande->getLigneCommandeListByCommande($request->getParameter('id'));
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new CommandeForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
	if (isset($_SESSION['panier']))
	{
		$panier = $_SESSION['panier']->getProduitList();
		$pos=0;
		
		$date = new DateTime();
		$date = date('Y-m-d');
		$com = new Commande();
		$com->setDateCreation($date);
		$com->setDatePaiement($date);
		$com->setStatut("validée");
		$com->setPersonne(null);
		$com->save();
		
		foreach ($panier as $produit):
			$ligne_com = new LigneCommande();
			$ligne_com->setProduit($produit['produit']);
			$ligne_com->setQuantite($produit['qte']);
			
			$produit['produit']->setStock($produit['produit']->getStock() - $ligne_com->getQuantite());
			$produit['produit']->save();
			
			$ligne_com->setValeur(($produit['produit']->getPrixHt() * $produit['qte']));
			$ligne_com->setCommande($com);
			$ligne_com->save();
		endforeach;
		$this->redirect('commande/index');
	}
	else
	{
		$this->forward('produit','index');
	}
	
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($commande = CommandePeer::retrieveByPk($request->getParameter('id')), sprintf('Object commande does not exist (%s).', $request->getParameter('id')));
    $this->form = new CommandeForm($commande);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($commande = CommandePeer::retrieveByPk($request->getParameter('id')), sprintf('Object commande does not exist (%s).', $request->getParameter('id')));
    $this->form = new CommandeForm($commande);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($commande = CommandePeer::retrieveByPk($request->getParameter('id')), sprintf('Object commande does not exist (%s).', $request->getParameter('id')));
    $commande->delete();

    $this->redirect('commande/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $commande = $form->save();

      $this->redirect('commande/edit?id='.$commande->getId());
    }
  }
}
